/**
 * 
 */
package vue.globalBar;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.session.ExecutableSessionBlock;

import vue.popup.IJPopupMenu;
import vue.popup.JPopupExecution;
import vue.session.IGExecutionBlock;
import controller.ISessionManager;

/**
 * A representation of a solver/preprocessor running for the GlobalBar. It is somehow associate to
 * {@link ExecutableSessionBlock}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGBarExecution extends IGGlobalBarItem implements MouseListener {
	private static final long	serialVersionUID	= 1L;

	public static final String	TIME_PREFIX			= "Time: ";
	public static final String	MEMORY_PREFIX		= "Mem: ";
	public static final String	STATE_PREFIX		= "State: ";


	public static final String	DEFAULT_DESCRIPTION	= "Execution";
	public static final String	UNKNOW				= "UNKNOW";


	protected JLabel			description;
	protected JLabel			state;
	protected JLabel			time;
	protected JLabel			memory;
	protected IJPopupMenu		rightMenu;


	protected IGExecutionBlock	exec				= null;

	/**
	 * Create an {@link IGBarExecution}.
	 * 
	 * @param exec
	 *            the {@link IGExecutionBlock} assiociate
	 * @param manager
	 *            the session manager
	 */
	public IGBarExecution(IGExecutionBlock exec, ISessionManager manager) {
		this.exec = exec;

		setLayout(new GridLayout(2, 1));
		
		description = new JLabel(DEFAULT_DESCRIPTION, JLabel.CENTER);
		state = new JLabel(STATE_PREFIX + UNKNOW, JLabel.CENTER);
		time = new JLabel(TIME_PREFIX + UNKNOW, JLabel.CENTER);
		memory = new JLabel(MEMORY_PREFIX + UNKNOW, JLabel.CENTER);

		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.add(description);
		description.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		panel.add(state);
		state.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		add(panel);
		
		panel = new JPanel(new GridLayout(1, 2));
		panel.add(time);
		time.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		panel.add(memory);
		memory.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		add(panel);

		rightMenu = new JPopupExecution(exec, manager);

		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		setSize(getPreferredSize());
		addMouseListener(this);
	}

	/**
	 * refresh the informations.
	 */
	public void refresh() {
		if (exec != null)
		{
			description.setText(exec.getExec().getDescritpion());
			state.setText(exec.getExec().getStatus().name());

			time.setText(TIME_PREFIX + UNKNOW);
			memory.setText(MEMORY_PREFIX + UNKNOW);

			// refresh pop up menu
			rightMenu.refresh();
		}
	}


	/**
	 * Return the {@link IGExecutionBlock} associate.
	 * 
	 * @return the {@link IGExecutionBlock} associate.
	 */
	public IGExecutionBlock getExec() {
		return exec;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		exec.notifySelected();
	}


	@Override
	public void mouseEntered(MouseEvent e) {
	}


	@Override
	public void mouseExited(MouseEvent e) {
	}


	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


}
